package cn.intotw.rdcj.task.spiders.client;

import cn.intotw.rdcj.task.spiders.netty.SpidersClientInboundHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.pool.AbstractChannelPoolHandler;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;

/**
 * Created by Chenxiang
 *
 * @generator: IntelliJ IDEA
 * @description: Netty的连接池Handler
 * @project: rdcj
 * @package: cn.intotw.rdcj.task.spiders.client
 * @date: 2020年07月31日 14时02分
 */
public class NettyChannelPoolHandler extends AbstractChannelPoolHandler {
    private SslContext sslCtx;

    public NettyChannelPoolHandler(SslContext sslCtx) {
        this.sslCtx = sslCtx;
    }

    @Override
    public void channelCreated(Channel channel) throws Exception {
        ChannelPipeline p=channel.pipeline();
        // Enable HTTPS if necessary.
        if (sslCtx != null) {
            p.addLast(sslCtx.newHandler(channel.alloc()));
        }

        p.addLast(new HttpClientCodec());

        // Remove the following line if you don't want automatic content decompression.
        p.addLast(new HttpContentDecompressor());

        // Uncomment the following line if you don't want to handle HttpContents.
        p.addLast(new HttpObjectAggregator(1048576));

        p.addLast(new SpidersClientInboundHandler());

        p.addLast(new LoggingHandler());
    }
}
